package com.atguigu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.UUID;

/**
 * @Author: liuaojun
 * @CreateTime: 2025-01-20
 * @Description:
 * @Version: 1.0
 */
@Controller
@RequestMapping("/file")
public class FileController {
    @RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("nickName") String nickName, @RequestParam("headPicture") MultipartFile multipartFile) throws Exception {
        String originalFilename = multipartFile.getOriginalFilename();
        String string = UUID.randomUUID().toString();
        String fileName = string + originalFilename;

        File file = ResourceUtils.getFile("classpath:static/");
        File fileDir = new File(file, "upload");
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        multipartFile.transferTo(new File(fileDir, fileName));
        return "upload";
    }
}
